package com.truthbean.jeetb.model.sys.repository;

import com.truthbean.jeetb.model.sys.entity.NewsType;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import javax.enterprise.inject.New;
import javax.persistence.*;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;
import javax.transaction.SystemException;
import javax.transaction.UserTransaction;
import java.util.List;

/**
 * Created by truth on 16-9-12.
 */
public class NewsTypeRepository {
    @PersistenceUnit
    private EntityManagerFactory emf;
    private EntityManager em = emf.createEntityManager();

    @Resource
    private UserTransaction utx;

    private CriteriaBuilder builder;

    @PostConstruct
    private void init(){
        builder = em.getCriteriaBuilder();
    }

    public void insert(NewsType type){
        try {
            utx.begin();
            em.persist(type);
            utx.commit();
        }catch (Exception e){
            try {
                utx.rollback();
            } catch (SystemException e1) {
                e1.printStackTrace();
            }
        }
    }

    public void update(NewsType type){
        try {
            utx.begin();
            em.merge(type);
            utx.commit();
        }catch (Exception e){
            try {
                utx.rollback();
            } catch (SystemException e1) {
                e1.printStackTrace();
            }
        }
    }

    public void delete(int id){
        try {
            utx.begin();
            em.remove(id);
            utx.commit();
        }catch (Exception e){
            try {
                utx.rollback();
            } catch (SystemException e1) {
                e1.printStackTrace();
            }
        }
    }

    public NewsType findOne(int id) {
        return em.find(NewsType.class, id);
    }

    public List<NewsType> findOpenTypePaged(int begin, int size) {
        Query query = em.createQuery("SELECT e FROM NewsType e where e.password is null", NewsType.class);
        query.setMaxResults(size);
        query.setFirstResult(begin);
        return query.getResultList();
    }

    /*fun findByParent(parent: String, begin: Int, size: Int): MutableList<NewsType> {
        val listCriteria = builder.createQuery(NewsType::class.java)
        val listRoot = listCriteria.from(NewsType::class.java)
        //listCriteria.where(builder.equal(listRoot.get(NewsTypeModel.group), group))
        listCriteria.select(listRoot)
        val query = em.createQuery(listCriteria)
        query.maxResults = size
        query.firstResult = begin
        return query.resultList
    }*/

    public List<NewsType> findAllPaged(int begin, int size) {
        CriteriaQuery<NewsType> criteriaQuery = builder.createQuery(NewsType.class);
        Root<NewsType> root = criteriaQuery.from(NewsType.class);

        criteriaQuery.select(root);

        TypedQuery<NewsType> typedQuery = em.createQuery(criteriaQuery);

        typedQuery.setMaxResults(size);
        typedQuery.setFirstResult(begin);
        return typedQuery.getResultList();
    }
    
}